import { _decorator, Component, EventHandler, instantiate, Label, Node, Sprite } from 'cc';
import { UICtrl } from '../../Ctrl/UICtrl';
import { Workshop } from '../../Entity/Component/SpawnerCom';
import { EconomicCom } from '../../Entity/Component/EconomicCom';

const { ccclass, property } = _decorator;

@ccclass('FactoryBuildItem')
export class FactoryBuildItem extends Component {

    @property({ type: Node, displayName: "icon挂载节点" })
    public icon_node: Node = null;
    @property({ type: Label, displayName: "待生产的数量" })
    public product_count: Label = null;
    @property({ type: Label, displayName: "存活的数量" })
    public live_count: Label = null;
    @property({ type: Sprite, displayName: "生产进程遮罩" })
    public progress_mask: Sprite = null;

    public workShop: Workshop = null;
    public economic: EconomicCom = null;
    public clickCallback: (workShop: Workshop) => void = null;

    protected update(dt: number): void {
        if (this.workShop) {
            this.product_count.string = (this.workShop.productNum || '') + '';
            this.progress_mask.fillRange = this.workShop.time / this.workShop.interval;
            if (this.economic && this.economic.isValid && this.live_count) {
                this.live_count.string = (this.economic.consumers_map.get(this.workShop.productType) || '') + '';
            }
        }
        // else {
        //     this.progress_mask.fillRange = 0;
        //     this.product_count.string = "0";
        //     if (this.icon_node.children.length > 0) {
        //         this.icon_node.destroyAllChildren();
        //     }
        // }
    }

    public OnClick(event: EventHandler, customEventData: string) {
        this.clickCallback && this.clickCallback(this.workShop);
    }
}


